package com.dikeleakagedetection.myapplication.utils

import com.dikeleakagedetection.myapplication.logic.model.RelativeDot

object InterpolationUtils {
    fun interpolation(start: List<RelativeDot>, width: Int, height: Int, amplification: Int): Array<DoubleArray>? {
        for (dot in start){
            dot.x = dot.x * amplification
            dot.y = dot.y * amplification
        }
        val res = Array(amplification * height + 1) {
            DoubleArray(
                amplification * width + 1
            )
        }
        for (m in 0 until amplification * height + 1) {
            for (n in 0 until amplification * width + 1) {
                res[m][n] = 0.0
                var D = 0.0
                var Dv = 0.0
                for (i in start.indices) {
                    val s0 = start[i]
                    if (m == s0.x && n == s0.y) {
                        res[m][n] = s0.value
                    } else {
                        val d: Double = 1.0 / ((s0.x - n) * (s0.x - n) + (s0.y - m) * (s0.y - m))
                        D += d
                        Dv += s0.value * d
                        res[m][n] = Dv / D
                    }
                }
            }
        }
        return res
    }
}